home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 41 / Amiga Format CD41 (1999-06)(Future Publishing)(GB)[!][issue 1999-07].iso / -seriously_amiga- / programming / other / gtlayout / source / ltp_creategadgets.c < prev    next >
C/C++ Source or Header  |  1999-04-19  |  8KB  |  317 lines

  1. /*
  2. **    GadTools layout toolkit
  3. **
  4. **    Copyright © 1993-1998 by Olaf `Olsen' Barthel
  5. **        Freely distributable.
  6. **
  7. **    :ts=4
  8. */
  9.  
  10. #ifndef _GTLAYOUT_GLOBAL_H
  11. #include "gtlayout_global.h"
  12. #endif
  13.  
  14. /*****************************************************************************/
  15.  
  16. #include <stdlib.h>
  17.  
  18. /*****************************************************************************/
  19.  
  20. #include "Assert.h"
  21.  
  22. /****************************************************************************/
  23.  
  24. static int compare(struct Gadget **a,struct Gadget **b)
  25. {
  26.     return(((int)a[0]->GadgetID) - ((int)b[0]->GadgetID));
  27. }
  28.  
  29. /****************************************************************************/
  30.  
  31. VOID
  32. LTP_CreateGadgets(LayoutHandle *Handle,struct IBox *Bounds,LONG Left,LONG Top,LONG PlusWidth,LONG PlusHeight)
  33. {
  34.     if(!Handle->Failed)
  35.     {
  36.         LONG    OldInterWidth    = Handle->InterWidth,
  37.                 OldInterHeight    = Handle->InterHeight;
  38.  
  39.         LONG    Attempt = 3,ScaleAttempt = 6;
  40.  
  41.         BOOL    Fits;
  42.         BOOL    Cloned = FALSE;
  43.  
  44.             // If the window is already open, do with it
  45.  
  46.         #ifdef DO_CLONING
  47.         {
  48.             if(Handle->Window)
  49.                 Handle->CloningPermitted = FALSE;
  50.         }
  51.         #endif
  52.  
  53.         do
  54.         {
  55.             LTP_LayoutGroup(Handle,Handle->TopGroup);
  56.  
  57.             LTP_PlaceGroups(Handle,Handle->TopGroup,Left,Top);
  58.  
  59.             Fits = TRUE;
  60.  
  61.             DB(kprintf("TopGroup->Width %ld + PlusWidth %ld = %ld > Bounds->Width %ld\n",Handle->TopGroup->Width,PlusWidth,Handle->TopGroup->Width + PlusWidth,Bounds->Width));
  62.  
  63.             if(!Handle->Window)
  64.             {
  65.                 if(Handle->TopGroup->Width + PlusWidth > Bounds->Width)
  66.                 {
  67.                     DB(kprintf("%ld > Screen->Width %ld\n",Handle->TopGroup->Width + PlusWidth,Handle->Screen->Width));
  68.  
  69.                     if(Handle->TopGroup->Width + PlusWidth > Handle->Screen->Width)
  70.                     {
  71.                         Fits = FALSE;
  72.  
  73.                         DB(kprintf("TopGroup->Height %ld + PlusHeight %ld = %ld > Bounds->Height %ld\n",Handle->TopGroup->Height,PlusHeight,Handle->TopGroup->Height + PlusHeight,Bounds->Height));
  74.  
  75.                         if(Handle->TopGroup->Height + PlusHeight > Bounds->Height)
  76.                         {
  77.                             DB(kprintf("rescale TRUE,TRUE\n"));
  78.  
  79.                             LTP_Rescale(Handle,TRUE,TRUE);
  80.                         }
  81.                         else
  82.                         {
  83.                             DB(kprintf("rescale TRUE,FALSE\n"));
  84.  
  85.                             LTP_Rescale(Handle,TRUE,FALSE);
  86.                         }
  87.  
  88.                         ScaleAttempt--;
  89.  
  90.                         Left            = Left            - OldInterWidth        + Handle->InterWidth;
  91.                         Top                = Top            - OldInterHeight    + Handle->InterHeight;
  92.                         PlusWidth        = PlusWidth        - OldInterWidth        + Handle->InterWidth;
  93.                         PlusHeight        = PlusHeight    - OldInterHeight    + Handle->InterHeight;
  94.                         OldInterWidth    = Handle->InterWidth;
  95.                         OldInterHeight    = Handle->InterHeight;
  96.                     }
  97.                 }
  98.                 else
  99.                 {
  100.                     DB(kprintf("TopGroup->Height %ld + PlusHeight %ld = %ld > Bounds->Height %ld\n",Handle->TopGroup->Height,PlusHeight,Handle->TopGroup->Height + PlusHeight,Bounds->Height));
  101.  
  102.                     if(Handle->TopGroup->Height + PlusHeight > Bounds->Height)
  103.                     {
  104.                         DB(kprintf("TopGroup->Height %ld + PlusHeight %ld = %ld > Screen->Height %ld\n",Handle->TopGroup->Height,PlusHeight,Handle->TopGroup->Height + PlusHeight,Handle->Screen->Height));
  105.  
  106.                         if(Handle->TopGroup->Height + PlusHeight > Handle->Screen->Height)
  107.                         {
  108.                             DB(kprintf("rescale FALSE,TRUE\n"));
  109.                             Fits = FALSE;
  110.  
  111.                             LTP_Rescale(Handle,FALSE,TRUE);
  112.  
  113.                             Left            = Left            - OldInterWidth        + Handle->InterWidth;
  114.                             Top                = Top            - OldInterHeight    + Handle->InterHeight;
  115.                             PlusWidth        = PlusWidth        - OldInterWidth        + Handle->InterWidth;
  116.                             PlusHeight        = PlusHeight    - OldInterHeight    + Handle->InterHeight;
  117.                             OldInterWidth    = Handle->InterWidth;
  118.                             OldInterHeight    = Handle->InterHeight;
  119.  
  120.                             ScaleAttempt--;
  121.                         }
  122.                     }
  123.                 }
  124.             }
  125.  
  126.             DB(kprintf("ok\n"));
  127.  
  128.             #ifdef DO_CLONING
  129.             {
  130.                 DB(kprintf("1) failed %ld cloned %ld cloningpermitted %ld\n",Handle->Failed,Cloned,Handle->CloningPermitted));
  131.  
  132.                 if(Handle->Failed && !Cloned && Handle->CloningPermitted)
  133.                 {
  134.                     if(LTP_PrepareCloning(Handle))
  135.                     {
  136.                         Bounds = &Handle->CloneExtra->Bounds;
  137.  
  138.                         Cloned = TRUE;
  139.                     }
  140.                 }
  141.             }
  142.             #endif
  143.  
  144.             if(Attempt > 0 && Fits && !Handle->Rescaled && !Cloned && !Handle->Failed && (Handle->GrowView || (Handle->ResizeObject && Handle->Window)))
  145.             {
  146.                 LONG            Width    = Handle->TopGroup->Width    + PlusWidth;
  147.                 LONG            Height    = Handle->TopGroup->Height    + PlusHeight;
  148.                 ObjectNode *    View;
  149.  
  150.                 Attempt--;
  151.  
  152.                 if(Handle->GrowView)
  153.                     View = Handle->GrowView;
  154.                 else
  155.                     View = Handle->ResizeObject;
  156.  
  157.                  if(Bounds->Width != Width || Bounds->Height != Height)
  158.                 {
  159.                     LONG PlusChars            = 0;
  160.                     LONG PlusLines            = 0;
  161.                     LONG AvailableWidth        = Bounds->Width        - Width;
  162.                     LONG AvailableHeight    = Bounds->Height    - Height;
  163.                     LONG GlyphWidth;
  164.                     LONG GlyphHeight;
  165.  
  166.                     /* Now that Handle->ResizeObject can be either a LISTVIEW_KIND or
  167.                      * a FRAME_KIND we have to watch closely which data we access.
  168.                      */
  169.                     if((View->Type == LISTVIEW_KIND) && (View->Special.List.TextAttr != NULL))
  170.                     {
  171.                         GlyphWidth    = View->Special.List.FixedGlyphWidth;
  172.                         GlyphHeight    = View->Special.List.FixedGlyphHeight;
  173.                     }
  174.                     else
  175.                     {
  176.                         GlyphWidth    = Handle->GlyphWidth;
  177.                         GlyphHeight    = Handle->GlyphHeight;
  178.                     }
  179.  
  180.                     if(Handle->GrowView != NULL)
  181.                     {
  182.                         if(View->Special.List.MaxGrowX > 0)
  183.                         {
  184.                             PlusChars = AvailableWidth / GlyphWidth;
  185.  
  186.                             if(PlusChars > 0 && View->Chars + PlusChars > View->Special.List.MaxGrowX)
  187.                                 PlusChars = View->Special.List.MaxGrowX - View->Chars;
  188.  
  189.                             if(PlusChars < 0)
  190.                                 PlusChars = 0;
  191.                         }
  192.                     }
  193.                     else
  194.                     {
  195.                         BOOLEAN CanResize;
  196.  
  197.                         if(View->Type == LISTVIEW_KIND)
  198.                             CanResize = View->Special.List.ResizeX;
  199.                         else
  200.                             CanResize = View->Special.Frame.ResizeX;
  201.  
  202.                         if(CanResize)
  203.                         {
  204.                             if(AvailableWidth < 0)
  205.                                 PlusChars = (AvailableWidth - (GlyphWidth + 1)) / GlyphWidth;
  206.                             else
  207.                                 PlusChars = AvailableWidth / GlyphWidth;
  208.                         }
  209.                     }
  210.  
  211.                     if(Handle->GrowView != NULL)
  212.                     {
  213.                         if(View->Special.List.MaxGrowY > 0)
  214.                         {
  215.                             PlusLines = AvailableHeight / GlyphHeight;
  216.  
  217.                             if(PlusLines > 0 && View->Lines + PlusLines > View->Special.List.MaxGrowY)
  218.                                 PlusLines = View->Special.List.MaxGrowY - View->Lines;
  219.  
  220.                             if(PlusLines < 0)
  221.                                 PlusLines = 0;
  222.                         }
  223.                     }
  224.                     else
  225.                     {
  226.                         BOOLEAN CanResize;
  227.  
  228.                         if(View->Type == LISTVIEW_KIND)
  229.                             CanResize = View->Special.List.ResizeY;
  230.                         else
  231.                             CanResize = View->Special.Frame.ResizeY;
  232.  
  233.                         if(CanResize)
  234.                         {
  235.                             if(AvailableHeight < 0)
  236.                                 PlusLines = (AvailableHeight - (GlyphHeight + 1)) / GlyphHeight;
  237.                             else
  238.                                 PlusLines = AvailableHeight / GlyphHeight;
  239.                         }
  240.                     }
  241.  
  242.                     if(PlusChars != 0 || PlusLines != 0)
  243.                     {
  244.                         View->Chars += PlusChars;
  245.                         View->Lines += PlusLines;
  246.  
  247.                         if(View->Type == FRAME_KIND)
  248.                         {
  249.                             View->Special.Frame.PlusWidth += PlusChars;
  250.                             if(View->Special.Frame.PlusWidth < 0)
  251.                                 View->Special.Frame.PlusWidth = 0;
  252.  
  253.                             View->Special.Frame.PlusHeight += PlusLines;
  254.                             if(View->Special.Frame.PlusHeight < 0)
  255.                                 View->Special.Frame.PlusHeight = 0;
  256.                         }
  257.  
  258.                         LTP_ResetGroups(Handle->TopGroup);
  259.  
  260.                         Fits = FALSE;
  261.                     }
  262.                 }
  263.  
  264.                 Handle->GrowView = NULL;
  265.             }
  266.  
  267.             DB(kprintf("fits %ld handle->failed %ld attempt %ld\n",Fits,Handle->Failed,Attempt));
  268.         }
  269.         while(!Fits && !Handle->Failed && ScaleAttempt > 0);
  270.  
  271.         #ifdef DO_CLONING
  272.         {
  273.             if(!Handle->Failed && !Cloned && Handle->CloningPermitted && !Fits && ScaleAttempt <= 0)
  274.             {
  275.                 if(LTP_PrepareCloning(Handle))
  276.                 {
  277.                     LTP_LayoutGroup(Handle,Handle->TopGroup);
  278.                     LTP_PlaceGroups(Handle,Handle->TopGroup,Left,Top);
  279.  
  280.                     Cloned = TRUE;
  281.                 }
  282.             }
  283.  
  284.             DB(kprintf("2) failed %ld cloned %ld\n",Handle->Failed,Cloned));
  285.  
  286.             if(!Handle->Failed && Cloned)
  287.                 LTP_CloneScreen(Handle,Handle->TopGroup->Width + PlusWidth,Handle->TopGroup->Height + PlusHeight);
  288.         }
  289.         #endif
  290.  
  291.         if(!Handle->Failed)
  292.         {
  293.             if(Handle->GadgetArray = LTP_Alloc(Handle,sizeof(struct Gadget *) * Handle->Count))
  294.             {
  295.                 #ifdef DO_PICKSHORTCUTS
  296.                 {
  297.                     if(!Handle->NoKeys)
  298.                     {
  299.                         LTP_SearchKeys(Handle,Handle->TopGroup);
  300.                         LTP_SelectKeys(Handle,Handle->TopGroup);
  301.                     }
  302.                 }
  303.                 #endif
  304.  
  305.                 Handle->Index = 0;
  306.  
  307.                 LTP_LayoutGadgets(Handle,Handle->TopGroup,Left,Top,PlusWidth,PlusHeight);
  308.  
  309.                 if(Handle->Failed)
  310.                     LTP_DisposeGadgets(Handle);
  311.                 else
  312.                     qsort(Handle->GadgetArray,Handle->Index,sizeof(struct Gadget *),compare);
  313.             }
  314.         }
  315.     }
  316. }
  317.